package com.d3works.apps.bankpayment.web;

import com.d3works.apps.bankpayment.db.BankReconciliation;
import com.d3works.apps.bankpayment.db.BankStatement;
import com.d3works.apps.bankpayment.db.repo.BankStatementRepository;
import com.d3works.apps.bankpayment.exception.IExceptionMessage;
import com.d3works.apps.bankpayment.service.bankreconciliation.BankReconciliationCreateService;
import com.d3works.apps.bankpayment.service.bankstatement.BankStatementRemoveService;
import com.d3works.apps.bankpayment.service.bankstatement.BankStatementService;
import com.d3works.common.ObjectUtils;
import com.d3works.exception.D3worksException;
import com.d3works.exception.ResponseMessageType;
import com.d3works.exception.db.repo.ExceptionOriginRepository;
import com.d3works.exception.service.TraceBackService;
import com.d3works.i18n.I18n;
import com.d3works.inject.Beans;
import com.d3works.meta.schema.actions.ActionView;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import com.google.common.base.Joiner;
import com.google.inject.Singleton;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;

@Singleton
public class BankStatementController {

  public void runImport(ActionRequest request, ActionResponse response) {
    try {
      BankStatement bankStatement = request.getContext().asType(BankStatement.class);

      BankStatementRepository bankStatementRepo = Beans.get(BankStatementRepository.class);
      BankStatementService bankStatementService = Beans.get(BankStatementService.class);
      bankStatement = bankStatementRepo.find(bankStatement.getId());
      bankStatementService.runImport(bankStatement, true);

    } catch (Exception e) {
      TraceBackService.trace(
          response, e, ExceptionOriginRepository.BANK_STATEMENT, ResponseMessageType.ERROR);
    }
    response.setReload(true);
  }

  public void print(ActionRequest request, ActionResponse response) {
    try {
      BankStatement bankStatement = request.getContext().asType(BankStatement.class);
      bankStatement = Beans.get(BankStatementRepository.class).find(bankStatement.getId());
      String name = bankStatement.getName();
      String fileLink = Beans.get(BankStatementService.class).print(bankStatement);
      response.setView(ActionView.define(name).add("html", fileLink).map());

    } catch (Exception e) {
      TraceBackService.trace(response, e, ExceptionOriginRepository.BANK_STATEMENT);
    }

    response.setReload(true);
  }

  public void runBankReconciliation(ActionRequest request, ActionResponse response) {

    try {
      BankStatement bankStatement = request.getContext().asType(BankStatement.class);
      bankStatement = Beans.get(BankStatementRepository.class).find(bankStatement.getId());
      List<BankReconciliation> bankReconciliationList =
          Beans.get(BankReconciliationCreateService.class)
              .createAllFromBankStatement(bankStatement);

      if (bankReconciliationList != null) {
        response.setView(
            ActionView.define(I18n.get("Bank reconciliations"))
                .model(BankReconciliation.class.getName())
                .add("grid", "bank-reconciliation-grid")
                .add("form", "bank-reconciliation-form")
                .param("search-filters", "bank-reconciliation-filters")
                .domain(
                    "self.id in ("
                        + Joiner.on(",")
                            .join(
                                bankReconciliationList.stream()
                                    .map(BankReconciliation::getId)
                                    .toArray())
                        + ")")
                .map());
      }
    } catch (Exception e) {
      TraceBackService.trace(response, e, ExceptionOriginRepository.BANK_STATEMENT);
    }
    response.setReload(true);
  }

  @SuppressWarnings("unchecked")
  public void deleteMultipleStatements(ActionRequest request, ActionResponse response) {
    try {
      List<Long> statementIds =
          (List)
              (((List) request.getContext().get("_ids"))
                  .stream()
                      .filter(ObjectUtils::notEmpty)
                      .map(input -> Long.parseLong(input.toString()))
                      .collect(Collectors.toList()));
      if (!CollectionUtils.isEmpty(statementIds)) {
        BankStatementRemoveService bankStatementRemoveService =
            Beans.get(BankStatementRemoveService.class);
        if (statementIds.size() == 1) {
          bankStatementRemoveService.deleteStatement(
              Beans.get(BankStatementRepository.class).find(statementIds.get(0)));
        } else {
          int errorNB = bankStatementRemoveService.deleteMultiple(statementIds);
          if (errorNB > 0) {
            response.setInfo(
                String.format(I18n.get(IExceptionMessage.STATEMENT_REMOVE_NOT_OK_NB), errorNB));
          } else {
            response.setInfo(I18n.get(IExceptionMessage.STATEMENT_REMOVE_OK));
            response.setReload(true);
          }
        }
      } else response.setInfo(I18n.get(IExceptionMessage.NO_STATEMENT_TO_REMOVE));
      response.setReload(true);
    } catch (Exception e) {
      TraceBackService.trace(
          response, e, ExceptionOriginRepository.BANK_STATEMENT, ResponseMessageType.ERROR);
    }
  }

  public void deleteStatement(ActionRequest request, ActionResponse response)
      throws D3worksException {
    try {
      BankStatement bankStatement = request.getContext().asType(BankStatement.class);
      bankStatement = Beans.get(BankStatementRepository.class).find(bankStatement.getId());

      Beans.get(BankStatementRemoveService.class).deleteStatement(bankStatement);

      response.setView(
          ActionView.define(I18n.get("Bank Statements"))
              .model(BankStatement.class.getName())
              .add("grid", "bank-statement-grid")
              .add("form", "bank-statement-form")
              .map());
      response.setCanClose(true);

    } catch (Exception e) {
      TraceBackService.trace(
          response, e, ExceptionOriginRepository.BANK_STATEMENT, ResponseMessageType.ERROR);
    }
  }
}
